package com.bocloud.cmp.driver.providers.jd;

import java.io.File;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import com.bocloud.cmp.driver.providers.ObjectDriver;
import com.bocloud.common.model.BsmResult;
import com.jcloud.jss.JingdongStorageService;
import com.jcloud.jss.domain.ObjectListing;
import com.jcloud.jss.domain.StorageObject;
import com.jcloud.jss.exception.StorageClientException;
import com.jcloud.jss.exception.StorageServerException;

public class JdObjectDriver extends JdDriver implements ObjectDriver {

	private static Logger logger = LoggerFactory.getLogger(JdObjectDriver.class);
	private JingdongStorageService jss;// 存储服务

	/**
	 * 
	 */
	public JdObjectDriver() {
	}

	/**
	 * @param accessKey
	 * @param secertkey
	 * @param endpoint
	 */
	public JdObjectDriver(String accessKey, String secretKey, String endpoint, String region) {
		super(accessKey, secretKey, endpoint, region);
		this.jss = new JingdongStorageService(this.getCredential().getJssCredential());
		this.jss.setEndpoint(endpoint);
	}

	/**
	 * @return the jss
	 */
	public JingdongStorageService getJss() {
		return jss;
	}

	/**
	 * @param jss
	 *            the jss to set
	 */
	public void setJss(JingdongStorageService jss) {
		this.jss = jss;
	}

	@Override
	public BsmResult exists(String bucket, String object) {
		try {
			boolean exist = jss.bucket(bucket).object(object).exist();
			return new BsmResult(exist, exist + "");
		} catch (StorageClientException | StorageServerException e) {
			logger.error("exists object [{}] exception!", e);
			return new BsmResult(false, "object [" + object + "] exists failed!");
		}
	}

	@Override
	public void destory() {
		if (null != jss) {
			jss.destroy();
		}
	}

	@Override
	public BsmResult remove(String bucket, String object) {
		if (StringUtils.isEmpty(bucket) || StringUtils.isEmpty(object)) {
			logger.info("bucket or object is empty");
			return new BsmResult(false, "bucket or name is empty!");
		}
		boolean exits = jss.bucket(bucket).object(object).exist();
		if (!exits) {
			logger.warn("object [{}] does not exists! ", object);
			return new BsmResult(false, "object [" + object + "] does not exists!");
		}
		try {
			jss.bucket(bucket).object(object).delete();
			boolean success = jss.bucket(bucket).object(object).exist();
			if (!success) {
				logger.info("remove object [{}] success!", object);
				return new BsmResult(true, "success");
			} else {
				logger.error("remove object [{}] failed!", object);
				return new BsmResult(false, "object [" + object + "] remove failed!");
			}
		} catch (StorageClientException | StorageServerException e) {
			logger.error("remove object [{}] exception!", e);
			return new BsmResult(false, "object [" + object + "] remove failed!");
		}
	}

	@Override
	public BsmResult detail(String bucket, String object) {
		if (StringUtils.isEmpty(bucket) || StringUtils.isEmpty(object)) {
			logger.info("bucket or object is empty");
			return new BsmResult(false, "bucket or name is empty!");
		}
		boolean exits = jss.bucket(bucket).object(object).exist();
		if (!exits) {
			logger.warn("object [{}] does not exists! ", object);
			return new BsmResult(false, "object [" + object + "] does not exists!");
		}

		try {
			StorageObject storageObject = jss.bucket(bucket).object(object).get();
			return new BsmResult(true, storageObject, "success");
		} catch (StorageClientException | StorageServerException e) {
			logger.error("Get object [{}] exception!", e);
			return new BsmResult(false, "Get object failed!");
		}
	}

	@Override
	public BsmResult list(String bucket) {
		try {
			ObjectListing objectList = jss.bucket(bucket).listObject();
			return new BsmResult(true, objectList.getCommonPrefixes(), "success");
		} catch (StorageClientException | StorageServerException e) {
			logger.error("Get object [{}] exception!", e);
			return new BsmResult(false, "Get object failed!");
		}
	}

	@Override
	public BsmResult createDir(String bucket, String object) {
		try {
			String defaultFile = "";
			jss.bucket(bucket).object(object).entity(new File(defaultFile)).put();
			return new BsmResult(true, "success");
		} catch (StorageClientException | StorageServerException e) {
			logger.error("Get object [{}] exception!", e);
			return new BsmResult(false, "Get object failed!");
		}
	}

	@Override
	public BsmResult uploadFile(String bucket, String object, String filepath) {
		// TODO Auto-generated method stub
		return null;
	}
}
